package com.example.testi18n.exception.global;

import com.example.testi18n.exception.BaseException;
import com.example.testi18n.exception.OrderServiceException;
import com.example.testi18n.exception.UserServiceException;
import com.example.testi18n.exception.constant.BaseThrowException;
import com.example.testi18n.exception.constant.OrderThrowException;
import com.example.testi18n.exception.constant.UserThrowException;
import com.example.testi18n.http.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaobendan
 * @version v1.0.0
 * @className JsonResult
 * @package com.example.testi18n.exception
 * @description 全局异常处理类
 * @date 2022-09-26 14:48
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

	/**
	 * 业务异常
	 */
	@ExceptionHandler(UserServiceException.class)
	public Object businessExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		log.error("[GlobalExceptionHandler][businessExceptionHandler] exception", e);
		UserServiceException be = (UserServiceException) e;
		// TODO
		String msg = be.getMessage();
		return JsonResult.error(be.getResponseEnum().getCode(), msg);
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public Object noHandlerFoundExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		log.error("[GlobalExceptionHandler][noHandlerFoundExceptionHandler] exception", e);
		String msg = BaseThrowException.PAGE_NOT_FOUND.getI18nMessage();
		return JsonResult.error(BaseThrowException.PAGE_NOT_FOUND.getCode(), msg);
	}

	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public Object httpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) {
		log.error("[GlobalExceptionHandler][httpRequestMethodNotSupportedException] exception", e);
		String msg = BaseThrowException.REQUEST_NOT_VALID.getI18nMessage();
		return JsonResult.error(BaseThrowException.REQUEST_NOT_VALID.getCode(), msg);
	}

	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Object methodArgumentNotValidException(MethodArgumentNotValidException e) {
		log.error("[GlobalExceptionHandler][methodArgumentNotValidException] exception", e);
		List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
		List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
		// return JsonResult.error(HttpStatus.BAD_REQUEST.value(), "参数检验失败", collect);
		return JsonResult.error(HttpStatus.BAD_REQUEST.value(), collect.get(0));
	}
	@ExceptionHandler(SQLException.class)
	public Object sqlExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		log.error("[GlobalExceptionHandler][sqlExceptionHandler] exception", e);
		String msg = BaseThrowException.SQL_ERROR.getI18nMessage();
		Integer code = BaseThrowException.SQL_ERROR.getCode();
		return JsonResult.error(code, msg);
	}
	/**
	 * 全局异常处理
	 */
	@ExceptionHandler(BaseException.class)
	public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		log.error("[GlobalExceptionHandler][exceptionHandler] exception", e);
		String msg = BaseThrowException.SERVER_ERROR.getI18nMessage();
		Integer code = BaseThrowException.SERVER_ERROR.getCode();
		return JsonResult.error(code, msg);
	}
}
